package aad.storyboard.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {

	// Database fields
	public static final String KEY_ROWID = "id";
	public static final String KEY_SCENE = "scenenumber";
	public static final String KEY_TIME = "time";
	public static final String KEY_DESCRIPTION = "description";
	private static final String DB_TABLE = "bookmark";  
	public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	private Context context;
	private SQLiteDatabase db;
	private DBHelper dbHelper;
	private SimpleDateFormat sdf;

	public DBAdapter(Context context) {
		this.context = context;
	}

	public DBAdapter open() throws SQLException {
		dbHelper = new DBHelper(context);
		db = dbHelper.getWritableDatabase();
		sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	/**
	 * Create a new bookmark If the bookmark is successfully created return the new
	 * rowId for that note, otherwise return a -1 to indicate failure.
	 */
	public long createBookmark(Integer scene, String description) {
		ContentValues values = createContentValues(scene, description, null);
		return db.insert(DB_TABLE, null, values);
	}
	/**
	 * Create a new bookmark If the bookmark is successfully created return the new
	 * rowId for that note, otherwise return a -1 to indicate failure.
	 */
	public long createBookmark(Integer scene, String description, Date time) {
	    String sTime = sdf.format(time);
		ContentValues values = createContentValues(scene, description, sTime);
		return db.insert(DB_TABLE, null, values);
	}

	/**
	 * Update the bookmark
	 */
	public boolean updateBookmark(long rowId, Integer scene, String description, Date time) {
	    String sTime = sdf.format(time);
		ContentValues values = createContentValues(scene, description, sTime);

		return db.update(DB_TABLE, values, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Deletes bookmark
	 */
	public boolean deleteBookmark(long rowId) {
		return db.delete(DB_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}
	

	/**
	 * Deletes bookmark
	 */
	public boolean deleteBookmark(Date time) {
	    String sTime = sdf.format(time);
		return db.delete(DB_TABLE, KEY_TIME + "=" + sTime,null) > 0;
	}
	

	/**
	 * Deletes bookmark
	 */
	public boolean deleteBookmark(String sTime) {
		return db.delete(DB_TABLE, KEY_TIME + "='" + sTime+"'",null) > 0;
	}

	/**
	 * Return a Cursor over the list of all bookmarks in the database
	 */
	public Cursor fetchAllBookmarks() {
		return db.query(DB_TABLE, new String[] { KEY_ROWID, KEY_SCENE,
				KEY_DESCRIPTION, KEY_TIME }, null, null, null, null, null);
	}

	/**
	 * Return a Cursor positioned at the defined bookmark
	 */
	public Cursor fetchBookmark(long rowId) throws SQLException {
		Cursor mCursor = db.query(true, DB_TABLE, new String[] {KEY_ROWID, KEY_SCENE,
				KEY_DESCRIPTION, KEY_TIME }, KEY_ROWID + "="
				+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	

	/**
	 * Return a Cursor positioned at the defined bookmark
	 */
	public Cursor fetchBookmark(String time) throws SQLException {
		Cursor mCursor = db.query(true, DB_TABLE, new String[] {KEY_ROWID, KEY_SCENE,
				KEY_DESCRIPTION, KEY_TIME }, KEY_TIME + "="
				+ time, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	private ContentValues createContentValues(Integer scene,
			String description,String time) {
		Calendar cal = Calendar.getInstance();
		String now = sdf.format(cal.getTime());
		
		ContentValues values = new ContentValues();
		values.put(KEY_SCENE, scene);
		if(description != null)
			values.put(KEY_DESCRIPTION, description);
		if(time == null || time.equals(""))
			time = now;
		values.put(KEY_TIME, time);
		return values;
	}

}
